﻿using System;
using System.Data;
using CrystalDecisions.CrystalReports.Engine;

namespace ArunimaErp.WebForm
{
    public partial class ReportForm : System.Web.UI.Page
    {
        private ReportDocument rptDoc = new ReportDocument();
        private rerpDataSet wipDs = new rerpDataSet(); // .xsd file name
        private DataTable wipDt = new DataTable();

        protected void LinkButton1_Click(object sender, EventArgs e)
        {
            Response.Redirect("/Home");
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            // Just set the name of data table
            wipDt.TableName = "wipTable";

            string _query = "select "
                                + "ps.PoStyleId, "
                                + "BuyerName, "
                                + "CustomerName, "
                                + "PoNo, "
                                + "StyleNo, "
                                + "BodyStyle, "
                                + "Febrication, "
                                + "DevisionName, "
                                + "SeasonName,"
                                + "FactoryName, "
                                + "Fob, "
                                + "AgreedCm, "
                                + "OrderQuantity, "
                                + "ExitDate, "
                                + "sum(TodaySewing) as TotalSewing, "
                                + "DeliveryDate, "
                                + "ProductionStartDate, "
                                + "RmReadyDate "
                                + "from BuyerInfo b, CustomerInfo c, Factory f, Season se, Devision d, StyleInfo s, "
                                + "PoStyle ps left join Mps m on ps.PoStyleId  = m.PoStyleId "
                                + "left join ProductionDailyReport dr on ps.PoStyleId = dr.PoStyleId "
                                + " where b.BuyerId = s.BuyerId and c.CustomerId = s.CustomerId and "
                                + " f.FactoryId = ps.FactoryId and s.StyleId = ps.StyleId and "
                                + " se.SeasonId = ps.SeasonId and d.DevisionId = s.DevisionId ";

            if (dateTimePicker1.Text == "" || dateTimePicker1.Text == "")
            {
                _query = _query + " group by ps.PoStyleId";
            }
            else
            {
                _query = _query +" ps.EntryDate between '" + dateTimePicker1.Text + "' and '" + dateTimePicker2.Text + "' "
                        + " group by ps.PoStyleId"
                ;
            }

            wipDt = DatabaseHelper.select(_query); //This function is located below this function
            wipDs.Tables["Wip"].Merge(wipDt);
            // Your .rpt file path will be below
            rptDoc.Load(Server.MapPath("../WebForm/WipReport.rpt"));

            //set dataset to the report viewer.
            rptDoc.SetDataSource(wipDs);
            CrystalReportViewer1.ReportSource = rptDoc;
        }

        protected void Page_Unload(object sender, EventArgs e)
        {
            rptDoc.Close();
            rptDoc.Dispose();
        }

        protected void LinkButton2_Click(object sender, EventArgs e)
        {
            Response.Redirect("/WebForm/OrderStatusForm.aspx");
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            // Just set the name of data table
            wipDt.TableName = "wipTable";

            string _query = "select "
                                + "ps.PoStyleId, "
                                + "BuyerName, "
                                + "CustomerName, "
                                + "PoNo, "
                                + "StyleNo, "
                                + "BodyStyle, "
                                + "Febrication, "
                                + "DevisionName, "
                                + "SeasonName,"
                                + "FactoryName, "
                                + "Fob, "
                                + "AgreedCm, "
                                + "OrderQuantity, "
                                + "ExitDate, "
                                + "sum(TodaySewing) as TotalSewing, "
                                + "DeliveryDate, "
                                + "ProductionStartDate, "
                                + "RmReadyDate "
                                + "from BuyerInfo b, CustomerInfo c, Factory f, Season se, Devision d, StyleInfo s, "
                                + "PoStyle ps left join Mps m on ps.PoStyleId  = m.PoStyleId "
                                + "left join ProductionDailyReport dr on ps.PoStyleId = dr.PoStyleId "
                                + " where b.BuyerId = s.BuyerId and c.CustomerId = s.CustomerId and "
                                + " f.FactoryId = ps.FactoryId and s.StyleId = ps.StyleId and "
                                + " se.SeasonId = ps.SeasonId and d.DevisionId = s.DevisionId ";

            if (dateTimePicker1.Text == "" || dateTimePicker1.Text == "")
            {
                _query = _query + " group by ps.PoStyleId";
            }
            else
            {
                _query = _query + " ps.EntryDate between '" + dateTimePicker1.Text + "' and '" + dateTimePicker2.Text + "' "
                        + " group by ps.PoStyleId"
                ;
            }

            wipDt = DatabaseHelper.select(_query); //This function is located below this function
            wipDs.Tables["Wip"].Merge(wipDt);
            // Your .rpt file path will be below
            rptDoc.Load(Server.MapPath("../WebForm/WipReport.rpt"));

            //set dataset to the report viewer.
            rptDoc.SetDataSource(wipDs);
            CrystalReportViewer1.ReportSource = rptDoc;
        }
    }
}